#include <func.h>

int main(int argc, char* argv[])
{
	// ./t_readdir dir 
	if (argc != 2) {
		error(1, 0, "Usage: %s dir", argv[0]);
	}

	// 1. 打开目录流
	DIR* dirp = opendir(argv[1]);
	if (dirp == NULL) {
		error(1, errno, "opendir %s", argv[1]);
	}

	// 2. 依次读取每一个目录项 (idiom)
	errno = 0;
	struct dirent* p;
	while ((p = readdir(dirp)) != NULL) {
		// 打印目录项
		printf("d_ino=%ld d_off=%ld d_reclen=%hu d_type=%u d_name=%s\n",
			   p->d_ino,
			   p->d_off,
			   p->d_reclen,
			   p->d_type,
			   p->d_name);
	} // p == NULL

	if (errno) {
		error(0, errno, "readdir");
	}

	// 3. 关闭目录流
	closedir(dirp);
	
    return 0;
}

